require 'rexml/document'
require 'c:/tools/kronos/ruby/fsm'
require 'c:/tools/kronos/ruby/interface'
require 'c:/tools/kronos/ruby/createfiles'
require 'c:/tools/kronos/ruby/multiPageFSM'

class VisioToCode

	attr_accessor :xml_doc
	attr_accessor :interface
	attr_accessor :fsm
	attr_accessor :interface_processed
	attr_accessor :fsm_processed
	attr_accessor :masters_processed

	def initialize( vdx_path )

		puts "Loading #{vdx_path} file. This may take a minute..."

		file = File.new vdx_path
		@xml_doc = REXML::Document.new file
		file.close

		@interface = nil
		@fsm				= Array.new

	end

	def seperatePages

		@xml_doc.root.elements["Pages"].each { |element|
			if "Interface" == element.attributes["Name"]
				@interface = element.elements["Shapes"]
			elsif nil != element.attributes["Name"].index("FSM")
				@fsm.push(element.elements["Shapes"])
				puts "Queued To Process #{element.attributes["Name"]} Page"
			else
				puts "Discarded For Processing #{element.attributes["Name"]} Page"
			end
		}

		@masters = @xml_doc.root.elements["Masters"]

	end

	def processMasters
		puts "Processing Masters"
		@masters_processed = Masters.new @masters
	end

	def processInterface

		puts "Processing Interface"
		@interface_processed = Interface.new @interface

	end

	def processFSM

		puts "Processing FSMs"
		@fsm_processed = MultiPageFSM.new @fsm, @masters_processed

	end

	def generateCode

		code = FSMCode.new @interface_processed, @fsm_processed

		# This is not needed anymore, put all in one file for compiler ease
		# code.createTablesFile
		code.createStatesFiles
		code.createFSMFile
		code.createContextFiles

	end

end
